<template>
  <div class="question-container">
    <el-card>
      <!-- 筛选 -->
      <el-row>
        <el-col :span="18">
          <el-form
            label-width="80px"
            size="small"
            :inline="true"
          >
            <el-form-item label="学科名称">
              <el-input
                style="200px"
                v-model="query.subjectName"
              ></el-input>
            </el-form-item>
            <el-form-item style="text-align:right">
              <el-button @click="clear">清除</el-button>
              <el-button
                type="primary"
                @click="search"
              >搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col
          :span="6"
          style="text-align:right"
        >
          <el-button
            icon="el-icon-edit"
            @click="newDialog()"
            type="success"
            size="small"
          >新增学科</el-button>
        </el-col>
      </el-row>
      <!-- 数据记录 -->
      <el-alert
        :title="`数据一共 ${total} 条`"
        style="margin-bottom:15px"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      ></el-alert>
      <!-- 表格 -->
      <el-table :data="subjects">
        <el-table-column
          label="序号"
          type="index"
        ></el-table-column>
        <el-table-column
          label="学科名称"
          prop="subjectName"
        ></el-table-column>
        <el-table-column
          label="创建者"
          prop="username"
        ></el-table-column>
        <el-table-column
          label="创建日期"
          width="160px"
        >

        </el-table-column>
        <el-table-column label="前台是否显示">
          <template slot-scope="scope">{{scope.row.isFrontDisplay===1?'是':'否'}}</template>
        </el-table-column>
        <el-table-column
          label="二级目录"
          prop="twoLevelDirectory"
        ></el-table-column>
        <el-table-column
          label="标签"
          prop="tags"
        ></el-table-column>
        <el-table-column
          label="题目数量"
          prop="totals"
        ></el-table-column>
        <el-table-column
          label="操作"
          width="240px"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="$router.push(`/subjects/directorys?id=${scope.row.id}&name=${scope.row.subjectName}`)"
            >学科分类</el-button>
            <el-button
              type="text"
              @click="$router.push(`/subjects/tags?id=${scope.row.id}&name=${scope.row.subjectName}`)"
            >学科标签</el-button>
            <el-button
              type="text"
              @click="newDialog(scope.row)"
            >修改</el-button>
            <el-button
              type="text"
              @click="deleteDiscipline(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="margin-top:20px;text-align:center"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :page-size="query.pagesize"
        :current-page="query.page"
        @current-change="pager"
        :page-sizes="[5,10,20,50]"
        @size-change="handleSizeChange"
      ></el-pagination>
    </el-card>
    <!-- 添加|修改 -->
    <subjects-add
      ref="subjectsadd"
      :data="currSubject"
      @updateList="updateList()"
    ></subjects-add>
  </div>
</template>

<script>
import { list as getSubjects, remove } from '@/api/hmmm/subjects'
import SubjectsAdd from '../components/subjects-add'
export default {
  name: 'SubjectsList',
  components: { SubjectsAdd },
  data () {
    return {
      query: {
        subjectName: '',
        page: 1,
        pagesize: 10
      },
      subjects: [], // 学科列表
      total: 0, // 总数
      currSubject: {}// 给子组件的
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 删除学科
    async deleteDiscipline (id) {
      await this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id })
        this.$message.success('删除成功')
      }).catch(() => { })
    },
    // 新增/修改框
    newDialog (id) {
      this.currSubject = id || {}
      this.$nextTick(() => {
        this.$refs.subjectsadd.open()
      })
    },
    // 新增更新列表
    updateList () {
      if (!this.currSubject.id) {
        this.query.page = 1
      }
      this.getList()
    },
    // 切换条数
    handleSizeChange (size) {
      this.query.pagesize = size
      this.getList()
    },
    // 切换分页
    pager (np) {
      this.query.page = np
      this.getList()
    },
    // 查询列表
    async getList () {
      const res = await getSubjects(this.query)
      console.log(111, res)
      this.subjects = res.data.items
      this.total = res.data.counts
    },
    // 清除
    clear () {
      this.query = {
        subjectName: '',
        page: 1,
        pagesize: 10
      }
      this.getList()
    },
    // 搜索
    search () {
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.fs {
  font-size: 12px;
}
</style>
